﻿using System;
using System.Windows.Media;

namespace DotNetUI.Models
{
    /// <summary>
    /// 颜色转换器
    /// </summary>
    public class ColorHelper
    {
        /// <summary>
        /// HSB转RGB
        /// </summary>
        /// <param name="hsb"></param>
        /// <returns></returns>
        public static Color Hsb2Rgb(Hsb hsb)
        {
            double r = 0;
            double g = 0;
            double b = 0;


            float sat = hsb.Saturation / 100.0f;
            float bri = hsb.Brightness / 100.0f;

            if (sat == 0)
            {
                r = g = b = bri;
            }
            else
            {
                // the color wheel consists of 6 sectors. Figure out which sector you're in.
                double sectorPos = hsb.Hue / 60.0;
                int sectorNumber = (int)(Math.Floor(sectorPos));
                // get the fractional part of the sector
                double fractionalSector = sectorPos - sectorNumber;

                // calculate values for the three axes of the color. 
                double p = bri * (1.0 - sat);
                double q = bri * (1.0 - (sat * fractionalSector));
                double t = bri * (1.0 - (sat * (1 - fractionalSector)));

                // assign the fractional colors to r, g, and b based on the sector the angle is in.
                switch (sectorNumber)
                {
                    case 0:
                        r = bri;
                        g = t;
                        b = p;
                        break;
                    case 1:
                        r = q;
                        g = bri;
                        b = p;
                        break;
                    case 2:
                        r = p;
                        g = bri;
                        b = t;
                        break;
                    case 3:
                        r = p;
                        g = q;
                        b = bri;
                        break;
                    case 4:
                        r = t;
                        g = p;
                        b = bri;
                        break;
                    case 5:
                        r = bri;
                        g = p;
                        b = q;
                        break;
                }
            }
            var red = Convert.ToByte(r * 255);
            var green = Convert.ToByte(g * 255);
            var blue = Convert.ToByte(b * 255); ;

            return Color.FromArgb(Convert.ToByte(255), red, green, blue);
        }
        /// <summary>
        /// RGB转 HSB
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static Hsb Rgb2Hsb(Color color)
        {
            System.Drawing.Color dColor = System.Drawing.Color.FromArgb(color.R, color.G, color.B);
            var hsb =new Hsb { Brightness =dColor.GetBrightness(), Hue = dColor.GetHue(), Saturation =dColor.GetSaturation() };
            return hsb;
        }
    }
}
